package com.ufenlei.LTS {
	import com.ufenlei.LTS.Input;
	import com.ufenlei.tool.RoundRectangle;

	import flash.events.Event;
	import flash.text.TextField;
	import flash.display.MovieClip;

	/**
	 * @author bbn
	 */
	public class ListItem extends Input{
		protected var _tf:TextField;
		private var _rr:RoundRectangle;
		private var _childList:List;
		private var _opened:Boolean = false;
		protected var _icon:MovieClip;
		
		public function ListItem(c:String = "ListItem",isopen:Boolean = false){
			_tf = new TextField();
			this._opened = isopen;
			this.addChild(_tf);
			_tf.height = 20;
			_tf.multiline = false;
			_rr = new RoundRectangle(_tf.width,_tf.height,0.1,0xffffff,0,0,0);
			this.addChild(_rr);
			_tf.x = 15;
			addEventListenerOn(_rr);
			this.text = c;
		}
		override protected function onClick():void{
			if( _childList && _childList.length > 0){
				this._opened?close():open();
			}
		}
		public function open():void{
			this.addChild(_childList);
			this._opened = true;
			this.initIcon();
			this.dispatchEvent(new Event(Input.CHANGE));
		}
		public function close():void{
			this.removeChild(_childList);
			this._opened = false;
			this.initIcon();
			this.dispatchEvent(new Event(Input.CHANGE));
		}
		public function get itemHeight():int{
			return _tf.height;
		}
		public function set text(c:String):void{
			_tf.text = c;
		}
		public function get text():String{
			return _tf.text;
		}
		public function addChildItem(item:ListItem):void{
			if(_childList == null){
				_childList = new List();
				_childList.y = this.height;
				_childList.x = _icon.width / 2;
				if(_opened) addChild(_childList);
			}
			_childList.addItem(item);
			this.dispatchEvent(new Event(Input.CHANGE));
			this.initIcon();
		}
		protected function initIcon():void{
			if( _childList && _childList.length > 0){
				this._opened?showOpened():showClosed();
			}else{
				this.showEnd();
			}
		}
		public function removeChildItem(item:ListItem):void {
			if(_childList){
				_childList.removeItem(item);
				this.dispatchEvent(new Event(Input.CHANGE));
			}
			this.initIcon();
		}
		private function showOpened():void{
			_icon.gotoAndStop('opened');
		}
		private function showClosed():void{
			_icon.gotoAndStop('closed');
		}
		private function showEnd():void{
			_icon.gotoAndStop('end');
		}
	}
}
